package xyz.xtt.equity.client.request;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import com.google.common.collect.Lists;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.NoArgsConstructor;
import xyz.xtt.asset.client.request.SendHttpResponse;

/**
 * @author huqibo
 * @date 2025/12/31
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DeilverCommonResponse {
	private TransferFromResult transferFromResult;
	/**
	 * http第三方响应
	 * 
	 * 第三方有可能调用多次
	 */
	@Default
	private List<SendHttpResponse> thirdResponse = new ArrayList<>();
	// 发元素，前一个响应
	private int index;

	public Boolean isTransferFromResult() {
		return Objects.equals(transferFromResult.getResult(), Boolean.TRUE);
	}

	public Boolean isThirdResponse() {
		return thirdResponse.stream().map(SendHttpResponse::getResult).filter(Objects::nonNull).allMatch(x -> Objects.equals(Boolean.TRUE, x));
	}

	public Boolean isOk() {
		return Stream.of(isTransferFromResult(), isThirdResponse()).allMatch(x -> Objects.equals(Boolean.TRUE, x));
	}

	/**
	 * 全失败
	 * 
	 * @return
	 */
	public Boolean isFall() {
		return Stream.of(isTransferFromResult(), isThirdResponse()).allMatch(x -> Objects.equals(Boolean.FALSE, x));
	}

	/**
	 * 有一个成功
	 * 
	 * @return
	 */
	public Boolean isAny() {
		return Stream.of(isTransferFromResult(), isThirdResponse()).anyMatch(x -> Objects.equals(Boolean.FALSE, x));
	}

	public static DeilverCommonResponse buildTransferFromResultFail() {
		TransferFromResult transferFromResult = TransferFromResult.builder().result(Boolean.FALSE).build();
		return DeilverCommonResponse.builder().transferFromResult(transferFromResult).build();
	}

	public static DeilverCommonResponse buildResult(TransferFromResult transferFromResult, List<SendHttpResponse> thirdResponse) {
		return DeilverCommonResponse.builder().transferFromResult(transferFromResult).thirdResponse(thirdResponse).build();
	}

	public static DeilverCommonResponse buildResult(TransferFromResult transferFromResult, SendHttpResponse thirdResponse) {
		if (Objects.isNull(thirdResponse)) {
			return buildResult(transferFromResult, Lists.newArrayList());
		}
		return buildResult(transferFromResult, Lists.newArrayList(thirdResponse));
	}

	public static DeilverCommonResponse buildResult(TransferFromResult transferFromResult, SendHttpResponse thirdResponse, int index) {
		if (Objects.isNull(thirdResponse)) {
			DeilverCommonResponse buildResult = buildResult(transferFromResult, Lists.newArrayList());
			buildResult.setIndex(index);
			return buildResult;
		}
		DeilverCommonResponse buildResult = buildResult(transferFromResult, Lists.newArrayList(thirdResponse));
		buildResult.setIndex(index);
		return buildResult;
	}

}
